package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.AppOrderDetailMapper;
import com.eco.fanliapp.dao.AppUserOrderMapper;
import com.eco.fanliapp.dao.SyUserUnboundedOrderApplyMapper;
import com.eco.fanliapp.entity.AppOrderDetail;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.AppUserOrder;
import com.eco.fanliapp.entity.SyUserUnboundedOrderApply;
import com.eco.fanliapp.service.IAppUserService;
import com.eco.fanliapp.service.ISyUserUnboundedOrderApplyService;
import com.eco.fanliapp.utils.DateUtils;
import com.eco.fanliapp.utils.EasyPoiStyleUtils;
import com.eco.fanliapp.utils.EasyPoiUtils;
import com.eco.fanliapp.utils.MapValueHelper;
import com.eco.fanliapp.vo.AppOrderDetailVo;
import com.eco.fanliapp.vo.SyChannelUserVO;
import com.eco.fanliapp.vo.SyUserUnboundedOrderApplyVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-12-19
 */
@Service
public class SyUserUnboundedOrderApplyServiceImpl extends ServiceImpl<SyUserUnboundedOrderApplyMapper, SyUserUnboundedOrderApply> implements ISyUserUnboundedOrderApplyService {

    @Autowired
    private SyUserUnboundedOrderApplyMapper syUserUnboundedOrderApplyMapper;

    @Override
    public ServerResponse addUnboundedOrder(Long orderId, AppUser user) {
        if (orderId == null) {
            return ServerResponse.createByErrorMessage("订单号不可为空");
        }
        if (orderId.toString().length() != 18) {
            return ServerResponse.createByErrorMessage("请输入正确的淘宝订单号");
        }
//        //根据relation_id，SpecialId查询用户
//        if (user.getRelationId() != null && user.getRelationId() != 0 && user.getSpecialId() != null && user.getSpecialId() != 0) {
//            return ServerResponse.createByErrorMessage("此订单");
//        }
        SyUserUnboundedOrderApply userUnboundedOrderApply = syUserUnboundedOrderApplyMapper.selectByUserIdAndOrderId(user.getUserId(), orderId);
        if (userUnboundedOrderApply != null) {
            if (userUnboundedOrderApply.getStatus() == 1) {
                return ServerResponse.createByErrorMessage("此订单已绑定成功");
            }
            return ServerResponse.createByErrorMessage("订单已提交过申请了!");
        }
        SyUserUnboundedOrderApply syUserUnboundedOrderApply = new SyUserUnboundedOrderApply();
        syUserUnboundedOrderApply.setUserId(user.getUserId());
        syUserUnboundedOrderApply.setOrderId(orderId);
        syUserUnboundedOrderApply.setStatus(0);
        syUserUnboundedOrderApply.setCommissionDealResult(0);
        int insert = syUserUnboundedOrderApplyMapper.insert(syUserUnboundedOrderApply);
        if (insert > 0) {
            return ServerResponse.createBySuccessMessage("提交成功,请等待审核!");
        }
        return ServerResponse.createByErrorMessage("此订单绑定失败");
    }

    @Override
    public ServerResponse selectUnboundedOrder(HashMap<String, Object> params) {
        MapValueHelper mapValueHelper = new MapValueHelper(params);
        int currentPage = mapValueHelper.getInt("currentPage");
        int pageSize = mapValueHelper.getInt("pageSize");
        PageHelper.startPage(currentPage, pageSize);
        List<SyUserUnboundedOrderApplyVo> list = syUserUnboundedOrderApplyMapper.selectUnboundedOrder(params);
        return ServerResponse.createBySuccess(new PageInfo(list));
    }

    @Override
    public List selectBatchTableId(List<Integer> list) {
        return syUserUnboundedOrderApplyMapper.selectBatchTableId(list);
    }

    @Override
    public void exportDetailList(HttpServletResponse response, HashMap<String, Object> params) {
        List<SyUserUnboundedOrderApplyVo> list = syUserUnboundedOrderApplyMapper.selectUnboundedOrder(params);
        //导出excel
        EasyPoiUtils.exportExcel(list, "订单绑定列表", "订单绑定列表", SyUserUnboundedOrderApplyVo.class, EasyPoiStyleUtils.class, "订单绑定详情统计.xls", response);
    }

    @Override
    public List<SyUserUnboundedOrderApply> selectOrderDetailBatch() {
        return syUserUnboundedOrderApplyMapper.selectOrderDetailBatch();
    }

    @Override
    public List<SyUserUnboundedOrderApply> selectBatchDate() {
        return syUserUnboundedOrderApplyMapper.selectBatchDate();
    }

    @Override
    public ServerResponse getUnboundedOrderList(AppUser user, Integer pageNum, Integer pageSize) {
        pageNum = (pageNum - 1) * pageSize;
        if (user != null && user.getUserId() != null) {
            List<SyUserUnboundedOrderApply> syUserUnboundedOrderList = syUserUnboundedOrderApplyMapper.getUnboundedOrderList(pageNum, pageSize, user.getUserId());
            if (syUserUnboundedOrderList == null || syUserUnboundedOrderList.size() == 0) {
                return ServerResponse.createBySuccessCodeMessage(201, "没有相关订单信息");
            } else {
                return ServerResponse.createBySuccess("查询成功", syUserUnboundedOrderList);
            }
        }
        return ServerResponse.createByErrorMessage("查询失败");
    }

}
